package com.ydgk.ajax;

import com.alibaba.fastjson.JSON;
import com.ydgk.bean.Student;
import com.ydgk.dao.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "StudentPagingAjaxServlet", urlPatterns = "/StudentPagingAjaxServlet.action")
public class StudentPagingAjaxServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          String pageNumber = request.getParameter("pageNumber");
          String pageCount = request.getParameter("pageCount");
        StudentDao studentDao = new StudentDao();
        studentDao.pageCount = Integer.parseInt(pageCount);
        int page = 0;
        try{
            page = Integer.parseInt(pageNumber);
        }catch (NumberFormatException e){
            page = 1;
        }
        int maxPage = studentDao.maxPage("student");
        if(page>maxPage){
            page = maxPage;
        }else if(page<0){
            page = 1;
        }
        List<Student> studentList = studentDao.paging(page);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("total",studentDao.total());
        map.put("pageNumber",page);
        map.put("pageCount",pageCount);
        map.put("data", studentList);
        String str = JSON.toJSONString(map);
        response.getWriter().print(str);
    }


}
